package com.cqc.fragmentreplace.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.cqc.fragmentreplace.fragment.Frag1;
import com.cqc.fragmentreplace.fragment.Frag2;
import com.cqc.fragmentreplace.fragment.Frag3;
import com.cqc.fragmentreplace.fragment.Frag4;
import com.cqc.fragmentreplace.R;

/**
 * Created by ${cqc} on 2017/4/19.
 */

public class AddHideShowActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Frag1 frag1;
    private Frag2 frag2;
    private Frag3 frag3;
    private Frag4 frag4;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_hide);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        btn1.performClick();
    }

    private void hideAllFragment(FragmentTransaction transaction) {
        if (frag1 != null) {
            transaction.hide(frag1);
        }
        if (frag2 != null) {
            transaction.hide(frag2);
        }
        if (frag3 != null) {
            transaction.hide(frag3);
        }
        if (frag4 != null) {
            transaction.hide(frag4);
        }
    }

    @Override
    public void onClick(View view) {
        transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (view.getId()) {
            case R.id.btn1:
                if(frag1 == null){
                    frag1 = new Frag1();
                    transaction.add(R.id.frame,frag1,"Frag1");
                }
                transaction.show(frag1);
                break;
            case R.id.btn2:
                if(frag2 == null){
                    frag2 = new Frag2();
                    transaction.add(R.id.frame,frag2,"Frag2");
                }
                transaction.show(frag2);
                break;
            case R.id.btn3:
                if(frag3 == null){
                    frag3 = new Frag3();
                    transaction.add(R.id.frame,frag3,"Frag3");
                }
                transaction.show(frag3);
                break;
            case R.id.btn4:
                if(frag4 == null){
                    frag4 = new Frag4();
                    transaction.add(R.id.frame,frag4,"Frag4");
                }
                transaction.show(frag4);
                break;
        }
        transaction.commit();
    }
}
